<template>
  <div class="form-container">
    <div class="page-bread">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/users/permission' }">权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{this.$route.params.permissionid?'编辑权限':'新增权限'}}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="form-content">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="form-wraper">
        <el-form-item label="权限ID" v-if="this.$route.params.permissionid">
          <span>{{this.$route.params.permissionid}}</span>
        </el-form-item>
        <el-form-item label="权限名称" prop="permission_name">
          <el-input v-model.trim="ruleForm.permission_name"></el-input>
        </el-form-item>
        <el-form-item label="权限描述" prop="desc">
          <el-input type="textarea" v-model.trim="ruleForm.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">{{this.$route.params.permissionid?'编辑权限':'立即创建'}}</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { createPermission, updatePermission, getById } from '@/api/permission'
export default {
  data() {
    return {
      ruleForm: {
        id: '',
        permission_name: '',
        desc: ''
      },
      rules: {
        permission_name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' },
          { min: 2, max: 64, message: '长度在 2 到 64 个字符', trigger: 'blur' }
        ],
        desc: [
          {
            min: 2,
            max: 128,
            message: '长度在 2 到 128 个字符',
            trigger: 'change'
          }
        ]
      }
    }
  },
  created() {
    if (this.$route.params.permissionid) this.loadData(this.$route.params.permissionid)
  },
  methods: {
    loadData(id) {
      getById({ id: id }).then(res => {
        Object.assign(this.ruleForm, res.data)
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.ruleForm.id && this.ruleForm.id > 0) {
            updatePermission(this.ruleForm).then(response => {
              if (response) {
                this.$notify({
                  title: '成功',
                  message: '修改成功',
                  type: 'success',
                  duration: 2000
                })
                this.$router.push({ path: '/users/permission' })
              }
            })
          } else {
            createPermission(this.ruleForm).then(response => {
              if (response) {
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 2000
                })
                this.$router.push({ path: '/users/permission' })
              }
            })
          }
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less">
.form-container {
  height: auto;
  overflow: hidden;

  .form-content {
    height: auto;
    overflow: hidden;
    padding: 15px;
    background: #fff;
    .form-wraper {
      width: 500px;
      .line {
        text-align: center;
      }
    }
  }
}
</style>